package com.iwinding.ladygg.bluetooth;

import com.iwinding.ladygg.constant.BluetoothConstants;
import com.iwinding.ladygg.utils.HexStringUtil;

/**
 * Created by hezhiyong on 2017/5/2.
 */

public class BluetoothApi {

    private static BluetoothApi instance;

    private OnBlueListener mOnBlueListener;

    public static BluetoothApi getInstance() {
        if (instance == null) {
            instance = new BluetoothApi();
        }
        return instance;
    }

    public void setmOnBlueListener(OnBlueListener mOnBlueListener) {
        this.mOnBlueListener = mOnBlueListener;
    }


    //握手
    public byte[] cmdHandshake() {
        return cmdGeneral(HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_HANDSHAKE));
    }

    //回复按钮状态
    public byte[] cmdKey3Press() {
        return cmdGeneral(HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_KEY3));
    }

    //回复角度信息
    public byte[] cmdDirection() {
        return cmdGeneral(HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_DIRECTION_ANGLE));
    }

    //回复电量
    public byte[] cmdPower() {
        return cmdGeneral(HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_POWER));
    }

    //修改音量
    public byte[] cmdVolume(byte volume) {
        return cmdGeneral(HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_VOLUME_MODIFY), new byte[]{volume});
    }

    private byte[] cmdGeneral(byte cmd, byte[] status) {
        return createCmd(cmd, status);
    }

    private byte[] cmdGeneral(byte cmd) {
        return createCmd(cmd, new byte[]{0x00});
    }

    //创建命令
    public byte[] createCmd(byte cmd, byte[] status) {
        byte[] send = new byte[16];

        send[0] = HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_HEAD);
        send[1] = HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_MID);
        send[2] = cmd;
        if (status != null && status.length < 12) {
            int j = 3;
            for (int i = 0; i < status.length; i++) {
                j = j + i;
                send[j] = status[i];
            }
            j = j + status.length;
            for (; j < 15; j++) {
                send[j] = 0x00;
            }
        }
        send[15] = HexStringUtil.charToByteString(BluetoothConstants.CMD.CMD_END);
        return send;
    }

    /**
     * 解析
     *
     * @param bytes
     */
    public void analysis(byte[] bytes) {
        String cmd = HexStringUtil.bytesToHexString(new byte[]{bytes[2]});
        switch (cmd) {
            case BluetoothConstants.CMD.CMD_HANDSHAKE:
                analysisHandshake(bytes);
                break;
            case BluetoothConstants.CMD.CMD_KEY3:
                analysisKey3(bytes);
                cmdKey3Press();
                break;
            case BluetoothConstants.CMD.CMD_DIRECTION_ANGLE:
                analysisDirection(bytes);
                cmdDirection();
                break;
            case BluetoothConstants.CMD.CMD_POWER:
                analysisPower(bytes);
                cmdPower();
                break;
            case BluetoothConstants.CMD.CMD_VOLUME_MODIFY_RE://0xff
                analysisVolume();
                break;
        }
    }

    /**
     * 解析握手命令返回
     *
     * @param bytes
     */
    void analysisHandshake(byte[] bytes) {
        boolean handshake = getHandshakeStatus(bytes[3]);
        boolean key3Press = getKey3Press(bytes[4]);
        boolean alarmStatus = getAlarmStatus(bytes[5]);
        boolean batteryTips = getBatteryTips(bytes[6]);
        int volume = getVolume(bytes[7]);
        if (mOnBlueListener != null)
            mOnBlueListener.onHandshakeStatus(handshake, key3Press, alarmStatus, batteryTips, volume);
    }

    /**
     * 解析Key3命令
     *
     * @param bytes
     */
    void analysisKey3(byte[] bytes) {
        boolean key3Press = getKey3Press(bytes[3]);
        if (mOnBlueListener != null)
            mOnBlueListener.onPress(key3Press);
    }

    /**
     * 解析角度命令
     *
     * @param bytes
     */
    void analysisDirection(byte[] bytes) {
        boolean alarmStatus = getAlarmStatus(bytes[3]);
        if (mOnBlueListener != null)
            mOnBlueListener.onDirection(alarmStatus);
    }

    /**
     * 解析电量命令
     *
     * @param bytes
     */
    void analysisPower(byte[] bytes) {
        boolean batteryTips = getBatteryTips(bytes[3]);
    }

    /**
     * 解析音量
     */
    void analysisVolume() {
        if (mOnBlueListener != null)
            mOnBlueListener.onVolume(volume);
    }


    /**
     * 获取握手状态
     *
     * @param status
     * @return
     */
    boolean getHandshakeStatus(int status) {
        return status == BluetoothConstants.DeviceStatus.HANDSHAKE_OK;
    }

    /**
     * key3按钮按下状态
     *
     * @param status
     * @return
     */
    boolean getKey3Press(int status) {
        return status == BluetoothConstants.DeviceStatus.KEY3_PRESS;
    }

    /**
     * 报警提示
     *
     * @param status
     * @return
     */
    boolean getAlarmStatus(int status) {
        return status == BluetoothConstants.DeviceStatus.DIRECTION_ALARM;
    }

    /**
     * 电量提示
     *
     * @param status
     * @return
     */
    boolean getBatteryTips(int status) {
        return status == BluetoothConstants.DeviceStatus.LOW_BATTERY_TIPS;
    }

    /**
     * 获取音量
     *
     * @param volume
     * @return
     */
    int getVolume(int volume) {
        return volume;
    }

    public interface OnBlueListener {
        public void onHandshakeStatus(boolean handshake, boolean press, boolean alarm, boolean tips, int volume);

        public void onDirection(boolean alarm);

        public void onPress(boolean press);

        public void onVolume(int volume);
    }

    private int volume;

    public void setVolume(int volume) {
        this.volume = volume;
    }
}
